package com.xhu.servlet;

import com.xhu.bean.pojo.Forecast;
import com.xhu.service.ForecastService;
import com.xhu.service.impl.ForecastServiceImpl;
import com.xhu.utils.BaseServlet;
import com.xhu.utils.FormatUtils;
import com.xhu.utils.ResultData;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@WebServlet("/forecastServlet/*")
public class ForecastServlet extends BaseServlet {
    public  ResultData  list(HttpServletRequest  request){
        //会接收到前端三个参数
        //page  limit    name
        Integer   page=FormatUtils.stringFormatInteger(request.getParameter("page"));
        Integer   limit=FormatUtils.stringFormatInteger(request.getParameter("limit"));
        String  name=FormatUtils.formatString(request.getParameter("name"));
        ForecastService forecastService=new ForecastServiceImpl();
        List<Forecast> list=forecastService.queryPage(page,limit,name);
        return  ResultData.ok().data(list);
    }

    public  ResultData  findAll(HttpServletRequest  request){
        ForecastService forecastService=new ForecastServiceImpl();
        List<Forecast> list=forecastService.findAll();
        return  ResultData.ok().data(list);
    }

    public   ResultData  add(HttpServletRequest request){
        //获取前端参数
        Forecast   forecast=new Forecast();
        try {
            Map map= request.getParameterMap();
            //Apache   BeanUtils  拷贝属性
            //把request中的请求参数 封装到forecast 属性中对象中
            //前端的请求的变量名要和后端的属性名保持一致
            BeanUtils.populate(forecast,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //把数据封装到对象中  然后传递对象即可
        ForecastService villageService=new ForecastServiceImpl();
        boolean   tf=villageService.add(forecast);
        return   ResultData.ok().msg(tf?"添加成功":"添加失败");
    }

    public   ResultData   findById(HttpServletRequest  request){
        Integer  id=FormatUtils.stringFormatInteger(request.getParameter("id"));
        ForecastService forecastService=new ForecastServiceImpl();
        Forecast  forecast=  forecastService.findById(id);
        return   ResultData.ok().data(forecast);
    }

    public  ResultData  update(HttpServletRequest  request){
        //获取前端所有字段并且封装成对象
        Forecast   forecast=new Forecast();
        try {
            Map  map= request.getParameterMap();
            BeanUtils.populate(forecast,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        ForecastService forecastService=new ForecastServiceImpl();
        boolean   tf=forecastService.update(forecast);
        return   ResultData.ok().msg(tf?"修改成功":"修改失败");
    }

    public  ResultData  deleteById(HttpServletRequest   request){
        Integer  id=FormatUtils.stringFormatInteger(request.getParameter("id"));
        ForecastService villageService=new ForecastServiceImpl();
        Boolean   tf=  villageService.deleteById(id);
        return   ResultData.ok().msg(tf?"删除成功":"删除失败");
    }

    public  ResultData  batchDelete(HttpServletRequest   request){
        String  ids=request.getParameter("ids");
        ForecastService forecastService=new ForecastServiceImpl();
        Boolean   tf=  forecastService.batchDelete(ids);
        return   ResultData.ok().msg(tf?"删除成功":"删除失败");
    }

    public  ResultData  findByNo(HttpServletRequest  request){
        String  forecast_month=request.getParameter("forecast_month");
        ForecastService forecastService=new ForecastServiceImpl();
        boolean   tf=  forecastService.findByMonth(forecast_month);
        return   ResultData.ok().msg(tf?"存在":"不存在").data(tf);
    }


}